package net.ycxinxing.mcpebox.ui;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import java.io.IOException;
import net.ycxinxing.mcpebox.mDialog;
import net.ycxinxing.mcpebox.DataCleanManager;
import android.app.ProgressDialog;

public class SettingActivity extends PreferenceActivity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)		
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		getPreferenceManager().setSharedPreferencesName("data");
		getPreferenceManager().setSharedPreferencesMode(MODE_PRIVATE);
		PreferenceScreen screen=getPreferenceManager().createPreferenceScreen(this);
		PreferenceCategory changgui=new PreferenceCategory(this);
		changgui.setTitle("常规");
		CheckBoxPreference safemode=new CheckBoxPreference(this);
		safemode.setTitle("安全模式(重启生效)");
		safemode.setKey("safemode");
		Preference reboot=new Preference(this);
		reboot.setTitle("重启");
		reboot.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
				@Override
				public boolean onPreferenceClick(Preference p1)
				{
					startActivity(new Intent(SettingActivity.this,RebotActivity.class));
					int pid = android.os.Process.myPid();
					String command = "kill " +pid;
					try {
						Runtime.getRuntime().exec(command);
					} catch (IOException e) {
						e.printStackTrace();
					}
					// TODO: Implement this method
					return false;
				}
			});
		PreferenceCategory AppData=new PreferenceCategory(this);
		AppData.setTitle("数据");
		Preference cleanCache=new Preference(this);
		cleanCache.setTitle("清除应用缓存");
		cleanCache.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
				@Override
				public boolean onPreferenceClick(Preference p1)
				{
					mDialog dlg=new mDialog(SettingActivity.this){
						@Override
						public void onButtonClick(int i)
						{
							if(i==2)
							{
								final ProgressDialog dialog=new ProgressDialog(SettingActivity.this);
								new Thread(){
									public void run()
									{
										DataCleanManager.clearAllCache(SettingActivity.this);
										dialog.dismiss();
									}
								}.start();
								dialog.setMessage("请稍后...");
								dialog.setCancelable(false);
								dialog.show();
							}
							// TODO: Implement this method
						}
					};
					try{
						dlg.setMsg("您有"+DataCleanManager.getTotalCacheSize(SettingActivity.this)+"缓存");
					}catch(Exception e)
					{
						dlg.setMsg(e.toString());
					}
					dlg.setButton3("确认清理");
					dlg.setButton2("取消");
					dlg.show();
					// TODO: Implement this method
					return false;
				}
			});
		screen.addPreference(changgui);
		screen.addPreference(safemode);
		screen.addPreference(reboot);
		screen.addPreference(AppData);
		screen.addPreference(cleanCache);
		setPreferenceScreen(screen);
	}
}
